#include <QDebug>
#include <QFileDialog>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->model = 0;
    ui->setupUi(this);

    this->setWindowTitle("QLearnPhrases");
    ui->pbLearn->setText("Learn text");
    ui->textBrowser->setReadOnly(false);
    ui->pbTalkMe->setText("Say me something");
    ui->plainTextEdit->setReadOnly(true);

    ui->tabWidget->setTabText(0, "Statistics");
    ui->tabWidget->setTabText(1, "Learn a phrase");
    ui->tabWidget->setTabText(2, "Results");

    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(onClickOpenFile()));
    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(onClickSaveFile()));
    connect(ui->actionLearn_text, SIGNAL(triggered()), this, SLOT(onClickLearnTextFromFile()));
    connect(ui->pbLearn, SIGNAL(clicked()), this, SLOT(onClickLearnText()));
    connect(ui->pbTalkMe, SIGNAL(clicked()), this, SLOT(onClickTalkMe()));
    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(onClickExit()));

    //ui->tabStadistics->
//    QStandardItem *group = new QStandardItem(QString("Group %1").arg(1));
//    for (int personnum = 0; personnum < 5 ; ++personnum)
//    {
//        QStandardItem *child = new QStandardItem(QString("Person %1 (group %2)").arg(personnum).arg(3));
//        /* the appendRow function appends the child as new row */
//        group->appendRow(child);
//    }
//    /* append group as new row to the model. model takes the ownership of the item */
//    model->appendRow(group);

    //FactoryNode *fn = new FactoryNode();

    //fn->addNode(new Node("pepe"));
    //fn->readTextFile("/home/shoulck/Plantillas/divertimento.txt");
    //fn->saveFile("/home/shoulck/Plantillas/kk.txt");
    //fn->loadFile("/home/shoulck/Plantillas/kk.txt");

    //fn->showNodes();

    //fn->readTextFile("/home/shoulck/Plantillas/divertimento.txt");
    //fn->saveFile("/home/shoulck/Plantillas/saved.txt");
    //fn->loadFile("/home/shoulck/Plantillas/saved.txt");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onClickOpenFile()
{
    qDebug() << "onClickOpenFile";    
    QFileDialog fd(this, "Load file..", ".", "QLearnPhrase Files (*.qlp)");
    if(fd.exec() == QDialog::Accepted)
    {
        QStringList list =  fd.selectedFiles();
        QString file = list.at(0); //Only get the first file.
        factory.loadFile(file);
        //factory.saveFile("/home/shoulck/Plantillas/saved.txt");
    }

    updateColumnView();
}

void MainWindow::onClickSaveFile()
{
    qDebug() << "onClickSaveFile";
    QFileDialog fd(this, "Save file..", ".", "QLearnPhrase Files (*.qlp)");
    fd.setAcceptMode(QFileDialog::AcceptSave);
    if(fd.exec() == QDialog::Accepted)
    {
        QStringList list =  fd.selectedFiles();
        QString file = list.at(0); //Only get the first file.
        qDebug() << file;
        if(factory.saveFile(file))
        {
            statusBar()->showMessage("New words have been saved sucefully");
        } else
        {
             statusBar()->showMessage("Error saving new words");
        }
    }
}

void MainWindow::onClickLearnTextFromFile()
{
    qDebug() << "onClickLearnTextFromFile";
    QFileDialog fd(this, "Load file..", ".", "Text plain Files (*.txt)");
    if(fd.exec() == QDialog::Accepted)
    {
        QStringList list =  fd.selectedFiles();
        QString file = list.at(0); //Only get the first file.
        qDebug() << file;
        factory.readTextFile(file);
        //factory.saveFile("/home/shoulck/Plantillas/saved.txt");
    }

    updateColumnView();
}

void  MainWindow::onClickLearnText()
{
    qDebug() << "onClickLearnText";
    factory.readText(ui->textBrowser->toPlainText());
    updateColumnView();
    ui->textBrowser->clear();
}

void MainWindow::onClickTalkMe()
{
    qDebug() << "onClickTalkMe";
    QString text = factory.makeText(50);
    if(text.length() == 0)
    {
        text = "Ups, I do not know not any word.";
    }
    ui->plainTextEdit->setPlainText(text);
}

void MainWindow::onClickExit()
{
    qDebug() << "onClickExit";
    close();
}

void MainWindow::updateColumnView()
{
    delete this->model;
    this->model = new QStandardItemModel();
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

    for(qint32 i = 0; i < factory.getCountWords(); i++)
    {
        Node* node = factory.getNode(i);
        if(node != 0)
        {
            QStandardItem *word = new QStandardItem(node->getKey());
            for(qint32 j = 0; j < node->getCountWords(); j++)
            {
                Node* nodeChild = node->getNode(j);
                QStandardItem *child = new QStandardItem(nodeChild->getKey());
                word->appendRow(child);
            }
            model->appendRow(word);
        }
    }
    proxyModel->setSourceModel(model);
    proxyModel->sort(0);
    ui->columnView->setModel(proxyModel);

    QString text;
    text += QString::number(factory.getCountWords()) + " words learned";
    statusBar()->showMessage(text);
}

